package org.example.service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.example.entity.Shanpin;
import org.example.entity.Shanpinfele;
import org.example.mapper.Shanpinfelemapper;
import org.example.result.PageResult;
import org.example.result.Pages;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShanpinfeleService {
    @Autowired
    Shanpinfelemapper shanpinfelemapper;


    /**
     * 商品分类分页查询
     * @param pages
     * @return
     */
    public PageResult pageQuery(Pages pages) {

        //开始分页查询
        PageHelper.startPage(pages.getCurrentPage(), pages.getPagesize());

        Page<Shanpinfele> page = shanpinfelemapper.pageQuery(pages);

        long total = page.getTotal();
        List<Shanpinfele> records = page.getResult();

        return new PageResult(total, records);
    }

    /**
     * 新增分类
     * @param shanpinfele
     */
    public void felexz(Shanpinfele shanpinfele) {

        shanpinfele.setStatus(1);
        shanpinfele.setZhongshu(0);
        shanpinfele.setCreateTime(LocalDateTime.now());
        shanpinfele.setUpdateTime(LocalDateTime.now());

        shanpinfelemapper.add(shanpinfele);
    }

    /**
     * 批量删除
     *
     * @param name
     * @param status
     * @param username
     */
    public int feleplsc(String name, String status, String username) {
        List<Shanpinfele>shanpinfeles=shanpinfelemapper.felelist(name,status,username);
       if(shanpinfeles.size()!=0)
           return 0;
       shanpinfelemapper.delete(name,status,username);
           return 1;
    }

    public Shanpinfele feleplcx(String name, String username) {

        return shanpinfelemapper.felecx(name,username);
    }

    /**
     * 修改分类
     * @param shanpinfele
     */
    public void felexg(Shanpinfele shanpinfele) {
        shanpinfele.setUpdateTime(LocalDateTime.now());
        shanpinfele.setStatus(-1);
        shanpinfelemapper.felexg(shanpinfele);
    }

    /**
     * 修改状态
     * @param shanpinfele
     */
    public void felezt(Shanpinfele shanpinfele) {
        if (shanpinfele.getStatus() == 1) {
            shanpinfele.setStatus(0);
        }
        else{
            shanpinfele.setStatus(1);
        }

        shanpinfelemapper.felexg(shanpinfele);
    }

    /**
     * 删除分类
     * @param id
     */
    public void felesc(int id) {
        shanpinfelemapper.felesc(id);
    }

    /**
     * name查询
     * @param username
     * @return
     */
    public String[] feleplnamecx(String username) {
        return shanpinfelemapper.felenamecx(username);
    }



}
